package cz.byteworks.android.myway.activities;

import java.text.SimpleDateFormat;
import java.util.List;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import cz.byteworks.android.myway.db.DataHelper;
import cz.byteworks.android.myway.map.MyWayItemizedOverlay;
import cz.byteworks.android.myway.map.MyWayPinItemizedOverlay;
import cz.byteworks.android.myway.pojo.Position;
import cz.byteworks.android.myway.pojo.Way;

public class MyWayMapActivity extends MapActivity {
	
	public static String ACTION_MAP_ACTIVITY = "actionMapActivity";
	private boolean isNormalMap;
	private MapView mapView;
	private GeoPoint startPoint;
	private GeoPoint endPoint;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		isNormalMap = true;
		Way way = (Way) getIntent().getExtras().getSerializable("wayObj");
		Position markTo = (Position) getIntent().getExtras().getSerializable("markTo");
		DataHelper dh = new DataHelper(this);
		List<Position> positionList = dh.getAllPositions(way.getId(), false);
		setContentView(R.layout.map);
		mapView = (MapView) findViewById(R.id.mapview);
	    mapView.setBuiltInZoomControls(true);
	    mapView.getController().setZoom(17);
		List<Overlay> mapOverlays = mapView.getOverlays();
		Drawable drawablePin = this.getResources().getDrawable(R.drawable.mappin);
		Drawable drawableStart = this.getResources().getDrawable(R.drawable.mappin_start);
		Drawable drawableEnd = this.getResources().getDrawable(R.drawable.mappin_end);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
		GeoPoint prePoint = null;
		GeoPoint curPoint = null;
		Drawable actualPin;
		MyWayPinItemizedOverlay pinOverlay = new MyWayPinItemizedOverlay(drawablePin);
		int index = 0;
		for (Position pos : positionList) {
			prePoint = curPoint;
			curPoint = new GeoPoint(new Double(pos.getLatitude()*1000000).intValue(), new Double(pos.getLongitude()*1000000).intValue());
			if (index == 0) {
				actualPin = drawableStart;
				if (markTo == null) {
					mapView.getController().animateTo(curPoint);
				}
				startPoint = curPoint;
			}
			else {
				if (index == positionList.size() - 1) {
					actualPin = drawableEnd;
				}
				else {
					actualPin = drawablePin;
				}
			}
			if (index == positionList.size() - 1) {
				endPoint = curPoint;
			}
			String note = "";
			if (pos.getLocality() != null) {
				note = note + pos.getLocality()+"\n";
			}
			if (pos.getNote() != null) {
				note = note + pos.getNote();
			}
			MyWayItemizedOverlay overlay = new MyWayItemizedOverlay(prePoint, curPoint, this, mapView);
			OverlayItem overlayitem = new OverlayItem(curPoint, sdf.format(pos.getLocateTime()), note);
			overlay.addOverlay(overlayitem);
			if (markTo == null || markTo.getPid() != pos.getPid()) {
				OverlayItem pinItem = new OverlayItem(curPoint, null, null);
				pinItem.setMarker(actualPin);
				pinOverlay.addOverlay(pinItem);
			}
			mapOverlays.add(overlay);
			index++;
		}	
		if (markTo != null) {
			GeoPoint geoMarkTo = new GeoPoint(new Double(markTo.getLatitude()*1000000).intValue(), new Double(markTo.getLongitude()*1000000).intValue());
			OverlayItem pinItem = new OverlayItem(geoMarkTo, null, null);
			pinItem.setMarker(getResources().getDrawable(R.drawable.map_pin_selected));
			pinOverlay.addOverlay(pinItem);
			mapView.getController().animateTo(geoMarkTo);
		}
		if (pinOverlay.size() > 0) {
			mapOverlays.add(pinOverlay);
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater mi = getMenuInflater();
		mi.inflate(R.menu.map_menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		switch (item.getItemId()) {
		case R.id.switch_map_view:
			if (isNormalMap) {
				mapView.setSatellite(true);
			}
			else {
				mapView.setSatellite(false);
			}
			isNormalMap = !isNormalMap;
			mapView.invalidate();
			break;
		case R.id.goto_start:
			if (startPoint != null) {
				mapView.getController().animateTo(startPoint);
			}
			break;
		case R.id.goto_end:
			if (endPoint != null) {
				mapView.getController().animateTo(endPoint);
			}
			break;
		}
		return true;
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

}
